// components/grade-list/grade-list.js
const setting = require("../../http/env");
const httpApi = require("../../http/request"); //后续可以改为你自己的域名接口地址

Component({
  /**
   * 组件的属性列表
   */
  properties: {},
  lifetimes: {
    attached() {
      this.getGrade();
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    //默认域名
    baseUrl: setting.baseUrl,
    gradeList: [],
    selectItem: {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getGrade() {
      httpApi.Post("/index/getGrade", {}).then((res) => {
        if (res.status) {
          return;
        }
        this.setData({
          gradeList: res.data,
        });
      });
    },
    selectGrade(e) {
      console.log(e);
      this.setData({
        selectItem: e.currentTarget.dataset.item,
      });
    },
    comfirm() {
      var that = this;
      if (!this.data.selectItem.id) {
        wx.showToast({
          title: "请先选择年级！",
          icon: "none",
          duration: 2000,
        });
      } else {
        httpApi
          .Post("/index/upGrade", {
            user_id: wx.getStorageSync("userInfo").id,
            grade_id: this.data.selectItem.id,
          })
          .then((res) => {
            var user = wx.getStorageSync("userInfo") || {};
            wx.setStorageSync("userInfo", Object.assign({}, user, res.data));
            this.triggerEvent("closeGrade", this.data.selectItem.id);
          });
      }
    },
  },
});
